<?php
/**
 * @version : 1.0
 * @package : lib/struct
 * @name :  CLanguage
 * @todo:  Lớp  cơ sở CLanguage. Xử lý ngôn ngữ
*/
class CLanguage
{
	static $current;
	public $language;
	public $files = array();
	public $words = array();
	function __construct($language = 'english')
	{
		$this->language = $language;
		$portal = CPortal::$components['PORTAL.'.portal()->id];
		while(true)
		{
			$this->import($portal['path'].'/lang/'.$language);
			if($portal['parent'] and isset(CPortal::$components['PORTAL.'.$portal['parent']]))
			{
				$portal = &CPortal::$components['PORTAL.'.$portal['parent']];
			}
			else
			{
				break;
			}
		}
	}
	function import($dir)
	{
		$file_name = $dir.'.lang.php';
		if(!isset($this->files[$dir]) and CFile::exists($file_name))
		{
			$this->files[$dir] =  1;
			eval('$this->words[] = '.file_get_contents($file_name).';');
		}
	}
	function get($word)
	{
		for($i=0; $i<sizeof($this->words); $i++)
		{
			if(isset($this->words[$i][$word]))
			{
				return $this->words[$i][$word];
			}
		}
		return $word;
	}
}

function language($word)
{
	if(CLanguage::$current)
	{
		return CLanguage::$current->get($word);
	}
	return $word;
}
function lang($word)
{
	return language($word);
}
?>